﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Wise.TimeWarrior.Helpers
{
    /// <summary>
    /// Directory Picker used in Configuration Form
    /// </summary>
    public class DirectoryPicker : UITypeEditor
    {

        IWindowsFormsEditorService editorService;
        FolderBrowserDialog picker = new FolderBrowserDialog();
        /// <summary>
        /// .ctor
        /// </summary>
        public DirectoryPicker()
        {


        }
        /// <summary>
        /// <see cref="UITypeEditor.EditValue(System.IServiceProvider,object)"/>
        /// </summary>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (this.editorService != null)
            {
                picker.ShowDialog();
                
                string dir= picker.SelectedPath;
                if(!string.IsNullOrEmpty(dir))
                {
                    value = dir;
                }
            }

            return value;

        }

        /// <summary>
        /// <see cref="UITypeEditor.GetEditStyle(ITypeDescriptorContext)"/>
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

    }


}
